package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.PrisonerTransferService;
import com.sinog.front.service.user.UserFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @desc 预安置管理 - 预安置人员迁移
 * @author wangxj
 * @date 2022/3/23 10:41
 */
@Slf4j
@RestController
public class PrisonerTransferController {

    @Autowired
    private UserFacade userService;
    @Autowired
    private PrisonerTransferService prisonerTransferService;

    /**
     * 预安置管理 - 预安置人员迁移 - 发起迁移/接收迁移列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param parameterMap parameterMap
     * @return String
     */
    @RequestMapping("/azbj/transfer/fqqy/info/list")
    public String getFqTranslateBaseInfoList(@RequestParam(required = false,defaultValue = "-1") Integer pageIndex,
                                             @RequestParam(required = false,defaultValue = "-1") Integer pageSize,
                                             @RequestParam(required = false) String sortField,
                                             @RequestParam(required = false) String sortOrder,
                                             @RequestParam(required = false) Map<String, Object> parameterMap) {
        UserDTO user = userService.getAuthentication();
        parameterMap.put("account",user.getAccount());
        parameterMap.put("level",user.getLevel());
        parameterMap.put("departid",user.getDepartid());
        parameterMap.put("sortField",sortField);
        parameterMap.put("sortOrder",sortOrder);
        return prisonerTransferService.getFqTranslateBaseInfoList(parameterMap,pageIndex,pageSize);
    }

    /**
     * 预安置管理 - 预安置人员迁移 - 添加页面 - 搜索条件(根据key模糊搜索)
     * @param key 服刑人员名称
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/transfer/fqqy/getPrisonerList")
    public List<Map<String,Object>> getPrisonerList(@RequestParam(required = false, defaultValue = "-1") String key) {
        if(StringUtils.equals(key,Constants.NumberStr.NUM_STR_FU_ONE)) {
            return null;
        }
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        String level = user.getLevel();
        return prisonerTransferService.getPrisonerList(key,departId,level);
    }

    /**
     * 反显服刑人员基本信息
     * @param priNum 服刑编号
     * @return Result
     */
    @RequestMapping("/azbj/transfer/fqqy/getBaseInfoByPriNumMap")
    public Result getBaseInfoByPriNumMap(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        Map<String,Object> prisonerMap = prisonerTransferService.getBaseInfoByPriNumMap(priNum);
        if(!prisonerMap.isEmpty()) {
            return Result.success(prisonerMap);
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 预安置管理 - 预安置人员迁移 - 发起迁移 - 添加页面 - 保存
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "新增/修改", value = "户籍迁移新增/更新")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/transfer/fqqy/savePrisonerTransfer")
    public Result savePrisonerTransfer(HttpServletRequest request,
                                       @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        Map<String,Object> jsonMap = JsonUtil.toMap(data);
        String priNum = (String)jsonMap.get("priNum");
        boolean mark = StringUtils.equals(priNum,"-1") || (null != priNum && priNum.isEmpty());
        if(mark) {
            return Result.failure(ReturnCode.SAVE_FAILED);
        }
        Map<String,Object> prisonerMap = prisonerTransferService.getBaseInfoByPriNumMap(priNum);
        ////服刑人员编号
        jsonMap.put("priNum",priNum);
        //姓名
        jsonMap.put("pname",prisonerMap.get("pName"));
        //服刑单位
        jsonMap.put("pdanwei",prisonerMap.get("pdanwei"));
        //服刑单位名称
        jsonMap.put("pdanweiname",prisonerMap.get("pdanweiName"));
        //迁入省
        jsonMap.put("nativeInpro",jsonMap.get("nativeinpro"));
        //迁入地区省name
        jsonMap.put("nativeInproName",jsonMap.get("nativeInproName"));
        //迁入地市
        jsonMap.put("nativeIncity",jsonMap.get("nativeincity"));
        //迁入地区地市name
        jsonMap.put("nativeIncityName",jsonMap.get("nativeIncityName"));
        //迁入区县
        jsonMap.put("nativeIncou",jsonMap.get("nativeincou"));
        //迁入地区区县name
        jsonMap.put("nativeIncouName",jsonMap.get("nativeIncouName"));
        //迁入乡镇
        jsonMap.put("nativeInvill",jsonMap.get("nativeinvill"));
        //迁入地区乡镇name
        jsonMap.put("nativeInvillName",jsonMap.get("nativeInvillName"));
        //迁出联系电话
        jsonMap.put("zpdanwei",jsonMap.get("zpdanwei"));
        //转监单位名称
        jsonMap.put("zpdanweiname",jsonMap.get("zpdanweiName"));
        //迁出人员联系固话
        jsonMap.put("qclxgh",jsonMap.get("fixedLine"));
        jsonMap.put("user",JsonUtil.toText(user));
        //审批状态
        jsonMap.put("state",null == jsonMap.get("state") ? Constants.Number.NUM_ONE : jsonMap.get("state"));
        //户籍所在省
        jsonMap.put("nativepro",null == jsonMap.get("nativepro") ? prisonerMap.get("nativepro") : jsonMap.get("nativepro"));
        //户籍所在地市
        jsonMap.put("nativecity",null == jsonMap.get("nativecity") ? prisonerMap.get("nativecity") : jsonMap.get("nativecity"));
        //户籍所在区县
        jsonMap.put("nativecou",null == jsonMap.get("nativecou") ? prisonerMap.get("nativecou") : jsonMap.get("nativecou"));
        //户籍所在乡镇
        jsonMap.put("nativevill",null == jsonMap.get("nativevill") ? prisonerMap.get("nativevill") : jsonMap.get("nativevill"));
        //户籍所在省name
        jsonMap.put("nativeproName",null == jsonMap.get("nativeproName") ? prisonerMap.get("nativeproName") : jsonMap.get("nativeproName"));
        //户籍所在地市name
        jsonMap.put("nativecityName",null == jsonMap.get("nativecityName") ? prisonerMap.get("nativecityName") : jsonMap.get("nativecityName"));
        //户籍所在区县name
        jsonMap.put("nativecouName",null == jsonMap.get("nativecouName") ? prisonerMap.get("nativecouName") : jsonMap.get("nativecouName"));
        //户籍所在乡镇name
        jsonMap.put("nativevillName",null == jsonMap.get("nativevillName") ? prisonerMap.get("nativevillName") : jsonMap.get("nativevillName"));
        if(Objects.equals(user.getLevel(),Constants.OrgLevel.LEVEL_QXSFJ)) {
            //户籍详细门牌号
            jsonMap.put("nativenum",prisonerMap.get("nativenum"));
        }
        if(Objects.equals(jsonMap.get("taskType"),Constants.NumberStr.NUM_STR_ONE)) {
            // 转监省份
            jsonMap.put("tranProvinces",jsonMap.get("nativepro"));
        }
        // 转监省份
        String dataStr = JsonUtil.toText(jsonMap);
        return prisonerTransferService.savePrisonerTransfer(dataStr);
    }

    /**
     * 预安置管理 - 预安置人员迁移 - 发起迁移 - 撤回
     * @param id transfet编号
     * @return Result
     */
    @Log(type = "更新", value = "户籍迁入、迁出申请，未审核的记录撤回")
    @Idempotent()
    @RequestMapping("/azbj/transfer/fqqy/toRevoke")
    public Result toRevoke(@RequestParam(required = false, defaultValue = "-1") String id) {
        if(StringUtils.equals(id,Constants.NumberStr.NUM_STR_FU_ONE)) {
            return Result.failure(ReturnCode.FAILURE);
        }
        return prisonerTransferService.toRevoke(id);
    }

    /**
     * 预安置管理 - 预安置人员迁移 - 发起迁移 - 删除(逻辑删除)
     * @param id transfet编号
     * @return Result
     */
    @Log(type = "更新", value = "户籍迁入、迁出申请，未审核的记录删除")
    @Idempotent()
    @RequestMapping("/azbj/transfer/fqqy/toDelete")
    public Result toDelete(@RequestParam(required = false, defaultValue = "-1") String id) {
        if(StringUtils.equals(id,Constants.NumberStr.NUM_STR_FU_ONE)) {
            return Result.failure(ReturnCode.FAILURE);
        }
        return prisonerTransferService.toDelete(id);
    }

    /**
     * 获取转监单位
     * @param code 当前省份标识
     * @param level 服刑单位(例、01:监狱,02:看守所,05:社矫中心)
     * @param departId 当前用户的departId
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/transfer/fqqy/getTransferUnit")
    public List<Map<String,Object>> getTransferUnit(
            @RequestParam(required = false, defaultValue = "-1") String code,
            @RequestParam(required = false, defaultValue = "-1") String level,
            @RequestParam(required = false, defaultValue = "-1") String departId) {
        return prisonerTransferService.getTransferUnit(code,level,departId);
    }

    /**
     * 查找此人是否正在户籍迁移
     * @param priNums priNums
     * @return Result
     */
    @RequestMapping("/azbj/transfer/fqqy/getTransferRecord")
    public Result getTransferRecord(@RequestParam(required = false, defaultValue = "-1") String... priNums) {
        List<String> priNumList = new ArrayList<>(10);
        for(String priNum : priNums) {
            priNumList.add(priNum);
        }
        return prisonerTransferService.getTransferRecord(priNumList);
    }
}